﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoodsInvoicing.Views.View;
using Spring.Context;
using GoodsInvoicing.Presentions.Presenter;
using Spring.Context.Support;
using GoodsInvoicing.BLL.Service;
using GoodsInvoicing.Model.UserRole;

namespace GoodsInvoicing.Views.ViewImpl
{
    public partial class UserListForm : Form, IUserListForm
    {
        private IList<GI_User> userlist = null;
        private string searchUserName = "";
        private int deleteUserID = 0;


        private IApplicationContext applicationContext;
        private IUserListFormPresenter presenter;
        private static UserListForm instance = new UserListForm();

        //使用单例模式
        private UserListForm()
        {
            InitializeComponent();

            applicationContext = ContextRegistry.GetContext();
            presenter = new UserListFormPresenter(this, applicationContext["GI_UserService"] as IGI_UserService);
            presenter.InitializeView();
        }

        public static UserListForm getInstance() 
        {
            return instance;
        }

        #region IUserListForm 成员
        public event EventHandler<EventArgs> UserListFormLoad;
        public event EventHandler<EventArgs> SearchButtonClick;

        public event EventHandler<EventArgs> UserDeleteClick;
        public event EventHandler<EventArgs> UserListFormClose;

        public IList<GI_User> UserList 
        {
            get 
            {
                return userlist;
            }
            set 
            {
                this.userlist = value;
            }
        }

        public string SearchUserName
        {
            get
            {
                return searchUserName;
            }
            set
            {
                this.searchUserName = value;
            }
        }

        public int DeleteUserID
        {
            get 
            {
                return deleteUserID;
            }
            set
            {
                this.deleteUserID = value;
            }
        }
        #endregion
        public void ShowUserAddForm()
        {
            throw new NotImplementedException();
        }

        public void ShowUserSaveForm()
        {
            throw new NotImplementedException();
        }

        public void UserListForm_Load(object sender, EventArgs e)
        {
            if (this.UserListFormLoad != null)
            {
                this.UserListFormLoad(sender, e);

                this.bsUserList.DataSource = this.UserList;
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                this.SearchUserName = this.tbUserName.Text.Trim();
                this.SearchButtonClick(sender, e);

                this.bsUserList.DataSource = this.UserList;
            }
        }

        /// <summary>
        /// 验证查询条件
        /// </summary>
        /// <returns></returns>
        private bool ValidateForm()
        {
            /*
            if (this.tbUserName.Text.Trim().Length == 0)
            {
                return false;
            }*/
            return true;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_Click(object sender, EventArgs e)
        {
            UserSaveForm saveForm = new UserSaveForm();
            //saveForm.WindowState = FormWindowState.Normal;
            saveForm.StartPosition = FormStartPosition.CenterScreen;
            saveForm.MdiParent = this.ParentForm;
            saveForm.TopMost = true;
            saveForm.Show();
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modify_Click(object sender, EventArgs e)
        {
            if (bsUserList.Current == null) 
            {
                MessageBox.Show("请先选择要修改的记录");
                return;
            }
            else
            {
                GI_User user = bsUserList.Current as GI_User;

                UserSaveForm saveForm = new UserSaveForm(user);
                saveForm.UserID = user.UserID;
                saveForm.UserName = user.UserName;
                saveForm.LoginName = user.LoginName;
                saveForm.LoginPWD = user.LoginPWD;
                saveForm.Telephone = user.Telephone;
                
                saveForm.MdiParent = this.ParentForm;
                saveForm.TopMost = true;
                saveForm.Show();
            }             
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            if (this.dgvUserList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择要删除的记录");
                return;
            }//此处应加入一个confirm弹出框
            else
            {
                DialogResult dr = MessageBox.Show("确定要删除该记录", "提示", MessageBoxButtons.YesNo);
                if (DialogResult.Yes == dr)
                {
                    foreach (DataGridViewRow row in this.dgvUserList.SelectedRows)
                    {
                        GI_User user = row.DataBoundItem as GI_User;

                        this.DeleteUserID = user.UserID;
                        this.UserDeleteClick(sender, e);
                    }
                    UserListForm_Load(sender, e);//刷新list
                }
            }
        }

        private void dgvUserList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                //获取控件的值
                if (Boolean.Parse(this.dgvUserList.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()))
                {
                    this.dgvUserList.Rows[e.RowIndex].Cells[0].Value = 0;
                    this.dgvUserList.Rows[e.RowIndex].Selected = false;
                }
                else
                {
                    this.dgvUserList.Rows[e.RowIndex].Cells[0].Value = 1;
                    this.dgvUserList.Rows[e.RowIndex].Selected = true;
                }
                
                //MessageBox.Show(this.dgvUserList.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()); //或者可以做其他事件处理程序
            }
        }
        


    }
}
